/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-06-28 02:01:44
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.audit.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.audit.bean.AuditLogRecordEditBean;
import com.joysuch.wwyt.audit.bean.AuditLogRecordListBean;
import com.joysuch.wwyt.audit.bean.AuditLogRecordListBean.SearchAuditLogRecord;
import com.joysuch.wwyt.audit.service.AuditLogRecordService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping("/bp/audit/log-record")
public class AuditLogRecordController {

    private static final Logger logger = LoggerFactory.getLogger(AuditLogRecordController.class);

    @Autowired
    private AuditLogRecordService auditLogRecordService;

    @RequestMapping("add")
    public Object add(@Valid @RequestBody AuditLogRecordEditBean entity) {
        auditLogRecordService.add(entity);
        return ResultBean.success("");
    }

    @RequestMapping("update")
    public Object update(@Valid @RequestBody AuditLogRecordEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        auditLogRecordService.update(entity);
        return ResultBean.success("");
    }


    @RequestMapping("page")
    public Object page(Pageable page, SearchAuditLogRecord condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<AuditLogRecordListBean> data = auditLogRecordService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("/his/page")
    public Object hisPage(Pageable page, AuditLogRecordListBean.SearchAuditLogHisRecord condition) {
        return auditLogRecordService.findByPageHis(page, condition);
    }


    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchAuditLogRecord condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<AuditLogRecordListBean> data = auditLogRecordService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("audit:log:record:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        auditLogRecordService.delete(id);
        return ResultBean.success("");
    }


    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(auditLogRecordService.getById(id));
    }

    @RequestMapping("moveData")
    public Object moveData(@Valid @NotNull(field = "endTime", message = "结束时间不可为空") Date endTime) {
        auditLogRecordService.handleMoveAuditLog(endTime);
        return ResultBean.defaultSuccessResult();
    }

}
